package com.coinland.study.window;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;

import com.coinland.study.util.DisplayUtils;

/**
 * 类描述:  以屏幕中某个点做缩小动画
 * 创建人:  shenZ
 * 创建时间: 2020/5/20
 * 联系方式: 252079249@qq.com
 * 修改时间: 2020/5/20
 */
public class PathShrinkView extends View {
    private Paint mPaint;
    private RectF mRectf;
    private Path mPath;

    enum TOP_BOTTOM {
        TOP, BOTTOM, NONE
    }

    // 中心点位置
    private float[] centerXY = new float[2];
    private Point mPoint = new Point();

    private float top, left, right, bottom;

    private float scale_tTob = 0.0f;

    // 状态栏StatusBar的高度
    private int sbHeight;
    // 当前位置
    private Point mCurrentPoint;
    private TOP_BOTTOM top_bottom;

    public PathShrinkView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
        sbHeight = DisplayUtils.getStatusBarHeight(context);
    }

    private void init() {
        // 画笔 初始化
        mPath = new Path();

        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#000000"));
        mPaint.setAntiAlias(true);//抗锯齿
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();

        // 在左侧位置
        if (centerXY[0] < getWidth() / 2) {
            left = 0;
            right = getWidth() - mCurrentPoint.x;
            if (top_bottom == TOP_BOTTOM.TOP) {
                top = 0;
                bottom = getHeight() - sbHeight - mCurrentPoint.y;
            } else if (top_bottom == TOP_BOTTOM.BOTTOM) {
                top = mCurrentPoint.y;
                bottom = getHeight() - sbHeight;
            } else {
                top = mCurrentPoint.y;
                bottom = getHeight() - sbHeight - mCurrentPoint.y / scale_tTob;
            }
        }
        // 在右侧位置
        else if (centerXY[0] > getWidth() / 2) {
            left = mCurrentPoint.x;
            right = getWidth();
            if (top_bottom == TOP_BOTTOM.TOP) {
                top = 0;
                bottom = getHeight() - sbHeight - mCurrentPoint.y;
            } else if (top_bottom == TOP_BOTTOM.BOTTOM) {
                top = mCurrentPoint.y;
                bottom = getHeight() - sbHeight;
            } else {
                top = mCurrentPoint.y;
                bottom = getHeight() - sbHeight - mCurrentPoint.y / scale_tTob;
            }
        }

        //绘制矩形
        mRectf = new RectF(left, top, right, bottom);
        mPath.addRoundRect(mRectf, 40, 40, Path.Direction.CW);

        canvas.drawPath(mPath, mPaint);
    }

    public void startAnim(IFinishValueAnim iFinishValueAnim) {
        ValueAnimator animator = ValueAnimator.ofObject(new PointEvaluator(),
                new Point(0, 0), mPoint);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(5000);
        animator.setRepeatCount(Animation.ZORDER_NORMAL);
        // AnimatorUpdateListener是监听ValueAnimaitor的值不断变化的过程
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurrentPoint = (Point) animation.getAnimatedValue();
                postInvalidate();
            }
        });

        // AnimatorListener主要是用来监听动画不同状态的监听器
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (iFinishValueAnim != null) {
                    iFinishValueAnim.finishAnim();
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animator.start();
    }


    /**
     * 设置中心点位置
     *
     * @param XY
     */
    public void setCenterXY(Point XY, float radius) {
        centerXY[0] = XY.x;
        centerXY[1] = XY.y;

        float spaceX_Left = centerXY[0];
        float spaceX_Right = getWidth() - centerXY[0] - 2 * radius;
        float spaceY_Top = centerXY[1] - sbHeight;
        float spaceY_Bottom = getHeight() - centerXY[1] - 2 * radius;

        if (spaceX_Left == 0) {
            mPoint.x = (int) spaceX_Right;
        } else if (spaceX_Right == 0) {
            mPoint.x = (int) spaceX_Left;
        }

        if (spaceY_Top == 0) {
            top_bottom = TOP_BOTTOM.TOP;
            mPoint.y = (int) spaceY_Bottom;
        } else if (spaceY_Bottom == 0) {
            top_bottom = TOP_BOTTOM.BOTTOM;
            mPoint.y = (int) spaceY_Top;
        } else {
            top_bottom = TOP_BOTTOM.NONE;
            mPoint.y = (int) spaceY_Top;
            scale_tTob = spaceY_Top / spaceY_Bottom;
        }

    }

    interface IFinishValueAnim {
        // 结束动画
        void finishAnim();
    }
}
